<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-calendar"></i> 用户管理
                </el-breadcrumb-item>
                <el-breadcrumb-item>添加用户</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="form-box">
                <el-form ref="param" :model="param"  :rules="rules" label-width="80px">
                    <el-form-item label="用户名" prop="username">
                        <el-input v-model="param.username" style="width:40% "></el-input>
                    </el-form-item>
                    <el-form-item label="密码" prop="userpassword">
                        <el-input v-model="param.userpassword" style="width:40% "></el-input>
                    </el-form-item>
                    <el-form-item label="性别" prop="usersex">
                        <el-select v-model="param.usersex" placeholder="请选择" style="width: 40%">
                            <el-option  label="男" value="男"></el-option>
                            <el-option  label="女" value="女" ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="用户身份" prop="roleid">
                        <el-select v-model="param.roleid" placeholder="请选择" style="width: 40%">
                            <el-option  label="教练" value="2" ></el-option>
                            <el-option  label="普通用户" value="3"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="联系电话" prop="usertel">
                        <el-input v-model="param.usertel" style="width:40% "></el-input>
                    </el-form-item>
                    <el-form-item label="电子邮箱" prop="useremail">
                        <el-input v-model="param.useremail" style="width:40% "></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="onSubmit">确定</el-button>
                        <el-button>取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import {addUser} from "../../api/admin";
    import {validateEmail, validatePhone,validateUsername,validatePassword,validateSex,validateRole} from "../../utils/validate";

    export default {
        name: 'baseform',
        data() {
            return {
                options: [
                ],
                param: {
                    username: '',
                    userpassword: '',
                    usersex: '',
                    usertel: '',
                    useremail:'',
                    roleid:''
                },
                rules:{
                    username: [{required:true,trigger:'blur',validator:validateUsername}],
                    userpassword:[{required:true,trigger:'blur',validator:validatePassword}],
                    usersex:[{required:true,trigger:'blur',validator:validateSex}],
                    roleid:[{required:true,trigger:'blur',validator:validateRole}],
                    usertel: [{required:true,trigger:'blur',validator:validatePhone}],
                    useremail:[{required:true,trigger:'blur',validator:validateEmail}]
                }
            };
        },
        methods: {
            onSubmit() {
                console.log(this.param);
                let user = JSON.parse(localStorage.getItem('user'));
                console.log(user)
                addUser(JSON.stringify({'username':this.param.username,'userpassword':this.param.userpassword,'usersex':this.param.usersex,'usertel':this.param.usertel,'useremail':this.param.useremail,'roleid':this.param.roleid,'operatorid':user.userid})).then(res => {
                    console.log(res);
                    if (res.state==2000) {
                        this.$message.success('添加成功！');
                        this.param.annotitle='';
                        this.param.annocontent='';
                        location.reload();
                    }else{
                        this.$message.error(res.message);
                    }

                })
            }
        }
    };
</script>
